package bitmap
{
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	public class BitmapAnimation
	{
		private var _frameObject:*;
		private var _frames:Vector.<BitmapFrame>;
		private var _labels:Array;
		private var _totalFrames:int;
		private var _bitmapFrameObjectType:String;
		private var _rectangle:Rectangle;
		private var _matrix:Matrix;
		private var _registerX:int ;
		private var _registerY:int ;
		private var _colorTransform:ColorTransform;
		
		/**
		 *  位图影片剪辑
		 * @param frameObject 源对象
		 * @param bitmapFrameObjectType 源对象类型
		 * 
		 */	
		public function BitmapAnimation(frameObject:*, bitmapFrameObjectType:String)
		{
			_frameObject=frameObject;
			_bitmapFrameObjectType=bitmapFrameObjectType;
			
			switch(bitmapFrameObjectType)
			{
				case BitmapFrameObjectType.MOVIE_CLIP:
					setMovieClip(frameObject as MovieClip);
					break;
				case BitmapFrameObjectType.BITMAP_FRAMES:
					_frames=frameObject;
					_totalFrames=_frames.length;
					break;
			}
		}
		
		/**
		 *合并绘制MovieClip
		 */		
		private function setMovieClip(movieClip:MovieClip):void
		{
			_frames=new Vector.<BitmapFrame>();
			_totalFrames=movieClip.totalFrames;
			_labels=movieClip.currentLabels;
			_matrix = new Matrix();
			var _colorTransform:ColorTransform = movieClip.transform.colorTransform ;
			for ( var i : int = 1 ; i <= _totalFrames ; i++)
			{
				movieClip.gotoAndStop (i);
				_rectangle = movieClip.getBounds(movieClip);
				_matrix.identity();
				_matrix.scale(movieClip.scaleX, movieClip.scaleY );
				_matrix.rotate(movieClip.rotation);
				if (movieClip.scaleX > 0)
				{
					_registerX = int(-_rectangle.x * movieClip.scaleX);
				}
				else
				{
					_registerX = int(-(_rectangle.width + _rectangle.x) * movieClip.scaleX);
				}
				if (movieClip.scaleY > 0)
				{
					_registerY = int(-_rectangle.y * movieClip.scaleY);
				}
				else
				{
					_registerY = int(-(_rectangle.height + _rectangle.y) * movieClip.scaleY);
				}
//								var k:int =  movieClip.numChildren;
//								for (var j:int = 0 ; j<k; ++j)
//								{
//									var child:MovieClip =  movieClip.getChildAt(j) as MovieClip;
//									if (child)
//									{
//										if ( child.currentFrame != child.totalFrames)
//										{
//											child.gotoAndStop(child.currentFrame+1);
//										}
//									}
//								}
				_matrix.translate(_registerX, _registerY);
				var bitmapData:BitmapData = new BitmapData(movieClip.width, movieClip.height, true, 0x000000);
				bitmapData.draw(movieClip, _matrix, _colorTransform);
				_frames.push(new BitmapFrame(bitmapData, _registerX, _registerY));
			}
			movieClip = null;
		}
		
		/**
		 *位图帧集 
		 */		
		public function get frames():Vector.<BitmapFrame>
		{
			return _frames;
		}
		
		/**
		 *总帧数
		 */		
		public function get totalFrames():int
		{
			return _totalFrames;
		}
		
		/**
		 *释放资源 
		 */	
		public function dispose():void
		{
			var frame:BitmapFrame;
			while (_frames.length > 0)
			{
				frame = _frames.shift();
				frame.dispose();
				frame=null;
			}
			frame=null;
			_frames = null;
			_frameObject=null;
			_labels=null;
			_rectangle=null;
			_matrix=null;
			_colorTransform=null;
		}
	}
}